{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d1f0258",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e9aa4e",
   "metadata": {},
   "source": [
    "# pandas案例导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b1056a6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change = np.random.normal(0,1,(10,5)) # 正太分布  # 均值,方差 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5b8bf2c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.66386649,  1.60834369,  0.49216098,  0.19383432,  1.04605588],\n",
       "       [ 0.13369183, -0.45151173, -0.55685675, -0.41018657, -0.44473787],\n",
       "       [-0.95600354,  1.40109402, -0.57336839,  0.29841438,  0.76228501],\n",
       "       [-0.40625607,  0.61502233,  0.94307196, -0.86905018, -0.86444782],\n",
       "       [-0.51327415,  0.21103334, -0.31152506, -0.09051696, -0.0925232 ],\n",
       "       [ 0.27232167,  1.17820723,  0.29090753,  0.2824525 ,  0.836843  ],\n",
       "       [-0.92573591, -0.74633987, -0.56333713,  0.17713305, -0.23184673],\n",
       "       [ 0.90787024, -0.33279108, -0.116468  , -0.1180384 ,  0.40660754],\n",
       "       [ 0.57233835, -0.59265088, -0.2634011 ,  1.15866477, -0.17530582],\n",
       "       [-0.49995871,  0.62660051,  1.27718278,  0.54461188, -0.41739722]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3c56ad43",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_rise = pd.DataFrame(stock_change)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e542b32b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4\n",
       "0 -0.663866  1.608344  0.492161  0.193834  1.046056\n",
       "1  0.133692 -0.451512 -0.556857 -0.410187 -0.444738\n",
       "2 -0.956004  1.401094 -0.573368  0.298414  0.762285\n",
       "3 -0.406256  0.615022  0.943072 -0.869050 -0.864448\n",
       "4 -0.513274  0.211033 -0.311525 -0.090517 -0.092523\n",
       "5  0.272322  1.178207  0.290908  0.282452  0.836843\n",
       "6 -0.925736 -0.746340 -0.563337  0.177133 -0.231847\n",
       "7  0.907870 -0.332791 -0.116468 -0.118038  0.406608\n",
       "8  0.572338 -0.592651 -0.263401  1.158665 -0.175306\n",
       "9 -0.499959  0.626601  1.277183  0.544612 -0.417397"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_rise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "95a9cbb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_rise.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e07ae08a",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_code = [\"股票{}\".format(i+1)for i in range(stock_rise.shape[0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d5ac2d01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9', '股票10']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cd7123ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票1</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票2</th>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票3</th>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票4</th>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票5</th>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票6</th>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票7</th>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票8</th>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票9</th>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票10</th>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0         1         2         3         4\n",
       "股票1  -0.663866  1.608344  0.492161  0.193834  1.046056\n",
       "股票2   0.133692 -0.451512 -0.556857 -0.410187 -0.444738\n",
       "股票3  -0.956004  1.401094 -0.573368  0.298414  0.762285\n",
       "股票4  -0.406256  0.615022  0.943072 -0.869050 -0.864448\n",
       "股票5  -0.513274  0.211033 -0.311525 -0.090517 -0.092523\n",
       "股票6   0.272322  1.178207  0.290908  0.282452  0.836843\n",
       "股票7  -0.925736 -0.746340 -0.563337  0.177133 -0.231847\n",
       "股票8   0.907870 -0.332791 -0.116468 -0.118038  0.406608\n",
       "股票9   0.572338 -0.592651 -0.263401  1.158665 -0.175306\n",
       "股票10 -0.499959  0.626601  1.277183  0.544612 -0.417397"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(stock_change,index=stock_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e4d68958",
   "metadata": {},
   "outputs": [],
   "source": [
    "date = pd.date_range(start=\"20190401\",periods=stock_rise.shape[1],freq=\"B\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b958d889",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2019-04-01', '2019-04-02', '2019-04-03', '2019-04-04',\n",
       "               '2019-04-05'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "936661b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c = pd.DataFrame(stock_change,index=stock_code,columns=date)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bada9def",
   "metadata": {},
   "source": [
    "# Dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2f9754ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-04-01</th>\n",
       "      <th>2019-04-02</th>\n",
       "      <th>2019-04-03</th>\n",
       "      <th>2019-04-04</th>\n",
       "      <th>2019-04-05</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票1</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票2</th>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票3</th>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票4</th>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票5</th>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票6</th>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票7</th>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票8</th>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票9</th>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票10</th>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      2019-04-01  2019-04-02  2019-04-03  2019-04-04  2019-04-05\n",
       "股票1    -0.663866    1.608344    0.492161    0.193834    1.046056\n",
       "股票2     0.133692   -0.451512   -0.556857   -0.410187   -0.444738\n",
       "股票3    -0.956004    1.401094   -0.573368    0.298414    0.762285\n",
       "股票4    -0.406256    0.615022    0.943072   -0.869050   -0.864448\n",
       "股票5    -0.513274    0.211033   -0.311525   -0.090517   -0.092523\n",
       "股票6     0.272322    1.178207    0.290908    0.282452    0.836843\n",
       "股票7    -0.925736   -0.746340   -0.563337    0.177133   -0.231847\n",
       "股票8     0.907870   -0.332791   -0.116468   -0.118038    0.406608\n",
       "股票9     0.572338   -0.592651   -0.263401    1.158665   -0.175306\n",
       "股票10   -0.499959    0.626601    1.277183    0.544612   -0.417397"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "ce83a6a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 5)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f618810f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9', '股票10'], dtype='object')"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "bb6f4172",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2019-04-01', '2019-04-02', '2019-04-03', '2019-04-04',\n",
       "               '2019-04-05'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "e5246d71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>股票1</th>\n",
       "      <th>股票2</th>\n",
       "      <th>股票3</th>\n",
       "      <th>股票4</th>\n",
       "      <th>股票5</th>\n",
       "      <th>股票6</th>\n",
       "      <th>股票7</th>\n",
       "      <th>股票8</th>\n",
       "      <th>股票9</th>\n",
       "      <th>股票10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-04-01</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.956004</td>\n",
       "      <td>-0.406256</td>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.272322</td>\n",
       "      <td>-0.925736</td>\n",
       "      <td>0.907870</td>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.499959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-02</th>\n",
       "      <td>1.608344</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>0.626601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-03</th>\n",
       "      <td>0.492161</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.277183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-04</th>\n",
       "      <td>0.193834</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>0.544612</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-04-05</th>\n",
       "      <td>1.046056</td>\n",
       "      <td>-0.444738</td>\n",
       "      <td>0.762285</td>\n",
       "      <td>-0.864448</td>\n",
       "      <td>-0.092523</td>\n",
       "      <td>0.836843</td>\n",
       "      <td>-0.231847</td>\n",
       "      <td>0.406608</td>\n",
       "      <td>-0.175306</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 股票1       股票2       股票3       股票4       股票5       股票6  \\\n",
       "2019-04-01 -0.663866  0.133692 -0.956004 -0.406256 -0.513274  0.272322   \n",
       "2019-04-02  1.608344 -0.451512  1.401094  0.615022  0.211033  1.178207   \n",
       "2019-04-03  0.492161 -0.556857 -0.573368  0.943072 -0.311525  0.290908   \n",
       "2019-04-04  0.193834 -0.410187  0.298414 -0.869050 -0.090517  0.282452   \n",
       "2019-04-05  1.046056 -0.444738  0.762285 -0.864448 -0.092523  0.836843   \n",
       "\n",
       "                 股票7       股票8       股票9      股票10  \n",
       "2019-04-01 -0.925736  0.907870  0.572338 -0.499959  \n",
       "2019-04-02 -0.746340 -0.332791 -0.592651  0.626601  \n",
       "2019-04-03 -0.563337 -0.116468 -0.263401  1.277183  \n",
       "2019-04-04  0.177133 -0.118038  1.158665  0.544612  \n",
       "2019-04-05 -0.231847  0.406608 -0.175306 -0.417397  "
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "4b6f7ae3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-04-01</th>\n",
       "      <th>2019-04-02</th>\n",
       "      <th>2019-04-03</th>\n",
       "      <th>2019-04-04</th>\n",
       "      <th>2019-04-05</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票1</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票2</th>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票3</th>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票4</th>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票5</th>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     2019-04-01  2019-04-02  2019-04-03  2019-04-04  2019-04-05\n",
       "股票1   -0.663866    1.608344    0.492161    0.193834    1.046056\n",
       "股票2    0.133692   -0.451512   -0.556857   -0.410187   -0.444738\n",
       "股票3   -0.956004    1.401094   -0.573368    0.298414    0.762285\n",
       "股票4   -0.406256    0.615022    0.943072   -0.869050   -0.864448\n",
       "股票5   -0.513274    0.211033   -0.311525   -0.090517   -0.092523"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.head() # 默认5行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "98af8638",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-04-01</th>\n",
       "      <th>2019-04-02</th>\n",
       "      <th>2019-04-03</th>\n",
       "      <th>2019-04-04</th>\n",
       "      <th>2019-04-05</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票6</th>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票7</th>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票8</th>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票9</th>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票10</th>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      2019-04-01  2019-04-02  2019-04-03  2019-04-04  2019-04-05\n",
       "股票6     0.272322    1.178207    0.290908    0.282452    0.836843\n",
       "股票7    -0.925736   -0.746340   -0.563337    0.177133   -0.231847\n",
       "股票8     0.907870   -0.332791   -0.116468   -0.118038    0.406608\n",
       "股票9     0.572338   -0.592651   -0.263401    1.158665   -0.175306\n",
       "股票10   -0.499959    0.626601    1.277183    0.544612   -0.417397"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.tail() # 默认5行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7c7b833",
   "metadata": {},
   "source": [
    "## 设置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "b2987ea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_code = [\"股票_{}\".format(i+1) for i in range(stock_rise.shape[0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "92d744c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c.index = stock_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b034b726",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-04-01</th>\n",
       "      <th>2019-04-02</th>\n",
       "      <th>2019-04-03</th>\n",
       "      <th>2019-04-04</th>\n",
       "      <th>2019-04-05</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票_1</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_2</th>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_3</th>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_4</th>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_5</th>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_6</th>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_7</th>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_8</th>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_9</th>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票_10</th>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       2019-04-01  2019-04-02  2019-04-03  2019-04-04  2019-04-05\n",
       "股票_1    -0.663866    1.608344    0.492161    0.193834    1.046056\n",
       "股票_2     0.133692   -0.451512   -0.556857   -0.410187   -0.444738\n",
       "股票_3    -0.956004    1.401094   -0.573368    0.298414    0.762285\n",
       "股票_4    -0.406256    0.615022    0.943072   -0.869050   -0.864448\n",
       "股票_5    -0.513274    0.211033   -0.311525   -0.090517   -0.092523\n",
       "股票_6     0.272322    1.178207    0.290908    0.282452    0.836843\n",
       "股票_7    -0.925736   -0.746340   -0.563337    0.177133   -0.231847\n",
       "股票_8     0.907870   -0.332791   -0.116468   -0.118038    0.406608\n",
       "股票_9     0.572338   -0.592651   -0.263401    1.158665   -0.175306\n",
       "股票_10   -0.499959    0.626601    1.277183    0.544612   -0.417397"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "00cb0bad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# stock_c.index[3] = \"hahah\" # error"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db4ca3b8",
   "metadata": {},
   "source": [
    "## 重设索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "f13b462e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>2019-04-01 00:00:00</th>\n",
       "      <th>2019-04-02 00:00:00</th>\n",
       "      <th>2019-04-03 00:00:00</th>\n",
       "      <th>2019-04-04 00:00:00</th>\n",
       "      <th>2019-04-05 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>股票_1</td>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>股票_2</td>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>股票_3</td>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>股票_4</td>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>股票_5</td>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>股票_6</td>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>股票_7</td>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>股票_8</td>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>股票_9</td>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>股票_10</td>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index  2019-04-01 00:00:00  2019-04-02 00:00:00  2019-04-03 00:00:00  \\\n",
       "0   股票_1            -0.663866             1.608344             0.492161   \n",
       "1   股票_2             0.133692            -0.451512            -0.556857   \n",
       "2   股票_3            -0.956004             1.401094            -0.573368   \n",
       "3   股票_4            -0.406256             0.615022             0.943072   \n",
       "4   股票_5            -0.513274             0.211033            -0.311525   \n",
       "5   股票_6             0.272322             1.178207             0.290908   \n",
       "6   股票_7            -0.925736            -0.746340            -0.563337   \n",
       "7   股票_8             0.907870            -0.332791            -0.116468   \n",
       "8   股票_9             0.572338            -0.592651            -0.263401   \n",
       "9  股票_10            -0.499959             0.626601             1.277183   \n",
       "\n",
       "   2019-04-04 00:00:00  2019-04-05 00:00:00  \n",
       "0             0.193834             1.046056  \n",
       "1            -0.410187            -0.444738  \n",
       "2             0.298414             0.762285  \n",
       "3            -0.869050            -0.864448  \n",
       "4            -0.090517            -0.092523  \n",
       "5             0.282452             0.836843  \n",
       "6             0.177133            -0.231847  \n",
       "7            -0.118038             0.406608  \n",
       "8             1.158665            -0.175306  \n",
       "9             0.544612            -0.417397  "
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "f9d93ce3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-04-01</th>\n",
       "      <th>2019-04-02</th>\n",
       "      <th>2019-04-03</th>\n",
       "      <th>2019-04-04</th>\n",
       "      <th>2019-04-05</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.663866</td>\n",
       "      <td>1.608344</td>\n",
       "      <td>0.492161</td>\n",
       "      <td>0.193834</td>\n",
       "      <td>1.046056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.133692</td>\n",
       "      <td>-0.451512</td>\n",
       "      <td>-0.556857</td>\n",
       "      <td>-0.410187</td>\n",
       "      <td>-0.444738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.956004</td>\n",
       "      <td>1.401094</td>\n",
       "      <td>-0.573368</td>\n",
       "      <td>0.298414</td>\n",
       "      <td>0.762285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.406256</td>\n",
       "      <td>0.615022</td>\n",
       "      <td>0.943072</td>\n",
       "      <td>-0.869050</td>\n",
       "      <td>-0.864448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.513274</td>\n",
       "      <td>0.211033</td>\n",
       "      <td>-0.311525</td>\n",
       "      <td>-0.090517</td>\n",
       "      <td>-0.092523</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.272322</td>\n",
       "      <td>1.178207</td>\n",
       "      <td>0.290908</td>\n",
       "      <td>0.282452</td>\n",
       "      <td>0.836843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.925736</td>\n",
       "      <td>-0.746340</td>\n",
       "      <td>-0.563337</td>\n",
       "      <td>0.177133</td>\n",
       "      <td>-0.231847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.907870</td>\n",
       "      <td>-0.332791</td>\n",
       "      <td>-0.116468</td>\n",
       "      <td>-0.118038</td>\n",
       "      <td>0.406608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.572338</td>\n",
       "      <td>-0.592651</td>\n",
       "      <td>-0.263401</td>\n",
       "      <td>1.158665</td>\n",
       "      <td>-0.175306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-0.499959</td>\n",
       "      <td>0.626601</td>\n",
       "      <td>1.277183</td>\n",
       "      <td>0.544612</td>\n",
       "      <td>-0.417397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   2019-04-01  2019-04-02  2019-04-03  2019-04-04  2019-04-05\n",
       "0   -0.663866    1.608344    0.492161    0.193834    1.046056\n",
       "1    0.133692   -0.451512   -0.556857   -0.410187   -0.444738\n",
       "2   -0.956004    1.401094   -0.573368    0.298414    0.762285\n",
       "3   -0.406256    0.615022    0.943072   -0.869050   -0.864448\n",
       "4   -0.513274    0.211033   -0.311525   -0.090517   -0.092523\n",
       "5    0.272322    1.178207    0.290908    0.282452    0.836843\n",
       "6   -0.925736   -0.746340   -0.563337    0.177133   -0.231847\n",
       "7    0.907870   -0.332791   -0.116468   -0.118038    0.406608\n",
       "8    0.572338   -0.592651   -0.263401    1.158665   -0.175306\n",
       "9   -0.499959    0.626601    1.277183    0.544612   -0.417397"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.reset_index(drop=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30a7f095",
   "metadata": {},
   "source": [
    "## 以某列设置为新的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "5ce7459c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({\n",
    "    \"month\":[1,4,7,10],\n",
    "    \"year\":[2012,2014,2015,2016],\n",
    "    \"sale\":[55,66,77,88]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "1680e292",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>year</th>\n",
       "      <th>sale</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2012</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>2014</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>2015</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>2016</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   month  year  sale\n",
       "0      1  2012    55\n",
       "1      4  2014    66\n",
       "2      7  2015    77\n",
       "3     10  2016    88"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "dc95b4ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>sale</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <th>1</th>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <th>4</th>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <th>7</th>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <th>10</th>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            sale\n",
       "year month      \n",
       "2012 1        55\n",
       "2014 4        66\n",
       "2015 7        77\n",
       "2016 10       88"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.set_index(keys=[\"year\",\"month\"]) # 三维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74f7cec7",
   "metadata": {},
   "source": [
    "## MultiIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "de80bc06",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pd.MultiIndex.from_tuples(\n",
    "    [('2023', 'Q1'), ('2023', 'Q2'), ('2024', 'Q1')],\n",
    "    names=['year', 'quarter']  # 给每个层级命名\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "0194e168",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series([100, 150, 200], index=index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "26b29945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year  quarter\n",
       "2023  Q1         100\n",
       "      Q2         150\n",
       "2024  Q1         200\n",
       "dtype: int64"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "0851b40a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([('2023', 'Q1'),\n",
       "            ('2023', 'Q2'),\n",
       "            ('2024', 'Q1')],\n",
       "           names=['year', 'quarter'])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "2242095d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FrozenList(['year', 'quarter'])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index.names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5eae74cc",
   "metadata": {},
   "source": [
    "## Series（一维数组）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27fb708e",
   "metadata": {},
   "source": [
    "### 创建series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "9b8c3828",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过ndarray创建\n",
    "arr = np.array([10, 20, 30])\n",
    "s = pd.Series(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "94914bcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    10\n",
       "1    20\n",
       "2    30\n",
       "dtype: int32"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "705ccbb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过索引创建\n",
    "s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])  # 索引为 'a','b','c'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "feea64a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    10\n",
       "b    20\n",
       "c    30\n",
       "dtype: int64"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "6e25a3b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过字典创建\n",
    "dict_data = {'a': 10, 'b': 20, 'c': 30}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "07d423a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series(dict_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "929a01b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    10\n",
       "b    20\n",
       "c    30\n",
       "dtype: int64"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f1561b5",
   "metadata": {},
   "source": [
    "### Series 的核心属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "0572c33f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c'], dtype='object')"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "3890df44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 20, 30], dtype=int64)"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea9ea93e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
